<template>
  <a-form :model="formState" ref="formDataRef" name="basic" :label-col="{ style: 'width:90px' }" :wrapper-col="{ style: 'calc(100% - 90px)' }" autocomplete="off" @finish="onFinish">
    <a-form-item label="费用名称" name="costName" :rules="[{ required: true, message: '请填写费用名称' }]">
      <a-input v-model:value="formState.costName" :maxlength="10" allowClear placeholder="请填写"></a-input>
    </a-form-item>

    <a-form-item label="单位" name="unit" :rules="[{ required: true, message: '请选择单位!' }]">
      <a-select v-model:value="formState.unit" placeholder="单位">
        <a-select-option value="票">票</a-select-option>
        <a-select-option value="件">件</a-select-option>
        <a-select-option value="方">方</a-select-option>
      </a-select>
    </a-form-item>

    <a-form-item label="单价" name="unitPrice" :rules="[{ required: true, message: '请填写价格!' }]">
      <a-input-number v-model:value="formState.unitPrice" :max="99999.99" :precision="2" placeholder="价格" />
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button type="primary" html-type="submit">确定</a-button>
      <a-button @click="cancelHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
import { toRefs, watch } from "vue";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: Base.GeneraDataType;
    type: number;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", visible: boolean): void;
  (e: "submit"): void;
}>();
const { visible, type, passData } = toRefs(props);
const { formState, formDataRef } = InitHook();
const { onFinish, resetFields, cancelHandle } = OperationHook({ formDataRef, formState, type, passData, emit });
watch(
  () => visible.value,
  (v: boolean) => {
    if (!v) {
      resetFields();
      formDataRef.value.clearValidate();
    }
  }
);
</script>
<style lang="stylus" scoped>
:deep(.ant-input-number){
  width: 100%;
}
</style>
